﻿using IntoLog;
using IntoStationSystem.Db;
using IntoStationSystem.Dialog;
using IntoStationSystem.Entity;
using IntoStationSystem.Global;
using IntoStationSystem.ViewModels;
using IntoStationSystem.Views;
using System;
using System.Windows;
using System.Windows.Controls;
using static IntoStationSystem.Global.Events;

namespace IntoStationSystem
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        MainWindowViewModel vm => Locator.Instance.MainWindowVM;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = vm;
            EventRepository.EventAggregator.GetEvent<StartCompletedEvent>().Subscribe(CompleteLoaded);
        }

        private void CompleteLoaded()
        {
            this.Visibility = Visibility.Visible;
            this.WindowState = WindowState.Maximized;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            vm.IsDisposed = true;
            this.Close();
        }

        private void Minisize_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            MainDialog dialog = new MainDialog("Login");
            dialog.Width /= 2;
            dialog.Height /= 2;
            LoginView login = new LoginView();
            dialog.main.Children.Add(login);
            dialog.ShowDialog();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //EventRepository.EventAggregator.GetEvent<PrintLabelEvent>().Publish(true);

            ProduceData iden = new ProduceData()
            {
                TrayId = "112212",
                DateTime = DateTime.Now,
            };
            DbHelper.LogProductRemote(iden, "OP10");
        }

        private void Menu_Click(object sender, RoutedEventArgs e)
        {
            string name = (sender as MenuItem).CommandParameter.ToString();
            Funcs.ShowUserControl(name);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Locator.Instance.Service.IsDisposed = true;
        }

        private void Test_Click(object sender, RoutedEventArgs e)
        {
            ProduceData data = new ProduceData();
            data.DateTime = DateTime.Now;
            data.TrayId = "1111";
            DbHelper.LogProductRemote(data);
        }
    }
}
